(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.lookup.splitAccountControllerView', splitAccountControllerView);
	splitAccountControllerView.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService' ];
	function splitAccountControllerView($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("splitAccountViewLookup"), data=[];
		ctrl.dialogsModule = dialogsModule;
		ctrl.config = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl.account) !== false) {
				$uibModalInstance.close();
			}
		};
		
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createInnerGridOptions($scope, ctrl.config.tableConfigs.table, rowSelectionChanged);
			ctrl.gridOptions.data = 'vm.account.splitAcct';
			ctrl.gridOptions.enableRowHeaderSelection = false;
			ctrl.account = {};
			ctrl.account.splitAcct = ctrl.dialogsModule.params.splitAcct || [];
			if(ctrl.account.splitAcct.length > 0){
				if(ctrl.account.splitAcct[0].patternflag){
					ctrl.account.patternflag = ctrl.account.splitAcct[0].patternflag;
				}
			}
			if(ctrl.dialogsModule.params.patternflag){
				ctrl.account.patternflag = ctrl.dialogsModule.params.patternflag;
			}
		    if(ctrl.account.patternflag =="Y"){
				ctrl.gridOptions.columnDefs[1].type = "text";
			}
			
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}
		ctrl.removeSplitAccount = function(){
			
			angular.forEach(ctrl.selectedRows, function(selectedParam, key) {
				angular.forEach(ctrl.account.splitAcct, function(param, index) {
					if (param.$$hashKey == selectedParam.$$hashKey) {
						ctrl.account.splitAcct.splice(index, 1);
					}
				});
			});
			
		};

	}
})(angular);
